package com.feather.tenia.adapter.saver;

import java.io.ByteArrayInputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import com.feather.common.data.Column;
import com.feather.common.db.DbSaver;

/**
 * @author flogyin
 */
public class ByteArray_BLOB implements DbSaver {
	@Override
	@SuppressWarnings("rawtypes")
	public Class getJavaType() {
		return byte[].class;
	}

	@Override
	public String getDbType() {
		return "blob";
	}

	@Override
	public int getSqlType() {
		return Types.BLOB;
	}

	@Override
	public boolean matchNull() {
		return true;
	}

	public boolean save(Object value, PreparedStatement statement, Column column) throws SQLException {
		// statement.setBytes(column.getOrdinal(), (byte[])value);
		if (value != null) {
			byte[] bytes = (byte[]) value;
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			statement.setBinaryStream(column.getOrdinal(), bais, bytes.length);
		} else {
			statement.setBytes(column.getOrdinal(), null);
		}
		return true;
	}
}
